/**
 * JZ UnoCSS - 渐变处理器
 * 处理渐变相关的类名组合和CSS生成
 * @version 1.0.0
 * @author jz
 */

import { generateCSS } from '../core/generator.js';
import { utils } from '../utils/utils.js';

/**
 * 处理渐变组合类名
 * @param {string[]} classNames - 类名数组
 * @param {Object} options - 选项
 * @returns {Object|null} 渐变信息对象
 */
export function processGradientClasses(classNames, options = {}) {
    // 查找渐变相关的类名
    let gradientDirection = null;
    let gradientFrom = null;
    let gradientTo = null;
    let gradientVia = null;
    
    const gradientClasses = [];
    
    for (const className of classNames) {
        // 解码类名用于检测
        const decodedClassName = utils.decodeClassNameFromUvue(className);
        
        // 检查是否是渐变相关类名
        if (decodedClassName.match(/^bg-gradient-to-/)) {
            const styles = generateCSS(decodedClassName, options);
            if (styles && styles['__gradient-direction']) {
                gradientDirection = styles['__gradient-direction'];
                gradientClasses.push(className);
            }
        } else if (decodedClassName.match(/^from-/)) {
            const styles = generateCSS(decodedClassName, options);
            if (styles && styles['__gradient-from']) {
                gradientFrom = styles['__gradient-from'];
                gradientClasses.push(className);
            }
        } else if (decodedClassName.match(/^via-/)) {
            const styles = generateCSS(decodedClassName, options);
            if (styles && styles['__gradient-via']) {
                gradientVia = styles['__gradient-via'];
                gradientClasses.push(className);
            }
        } else if (decodedClassName.match(/^to-/)) {
            const styles = generateCSS(decodedClassName, options);
            if (styles && styles['__gradient-to']) {
                gradientTo = styles['__gradient-to'];
                gradientClasses.push(className);
            }
        }
    }
    
    // 如果没有渐变方向或起始颜色，不处理渐变
    if (!gradientDirection || !gradientFrom) {
        return null;
    }
    
    // 生成渐变CSS
    let gradientValue;
    if (gradientVia && gradientTo) {
        // 三色渐变
        gradientValue = `linear-gradient(${gradientDirection}, ${gradientFrom}, ${gradientVia}, ${gradientTo})`;
    } else if (gradientTo) {
        // 两色渐变
        gradientValue = `linear-gradient(${gradientDirection}, ${gradientFrom}, ${gradientTo})`;
    } else {
        // 单色到透明渐变
        gradientValue = `linear-gradient(${gradientDirection}, ${gradientFrom}, transparent)`;
    }
    
    // 生成组合选择器（使用编码后的类名）
    const selector = gradientClasses.map(cls => `.${cls}`).join('');
    
    // 根据模式决定CSS格式
    const isProduction = options.mode === 'production';
    const styleRule = isProduction 
        ? `${selector}{background-image:${gradientValue}}`
        : `${selector} {\n  background-image: ${gradientValue};\n}`;
    
    return {
        selector: gradientClasses.join('.'),
        styleRule: styleRule,
        classNames: gradientClasses,
        gradientValue: gradientValue
    };
}

/**
 * 生成条件编译渐变CSS
 * @param {Object} gradientInfo - 渐变信息
 * @param {Object} options - 选项
 * @returns {string} 带条件编译的渐变CSS
 */
export function generateConditionalGradientCSS(gradientInfo, options = {}) {
    const { mode = 'development' } = options;
    const isProduction = mode === 'production';
    const selector = gradientInfo.selector;
    
    // 使用预计算的渐变值
    const gradientValue = gradientInfo.gradientValue;
    
    // 渐变在所有平台都是通用的，不需要条件编译
    if (isProduction) {
        return `.${selector}{background-image:${gradientValue}}`;
    } else {
        return `.${selector} {\n  background-image: ${gradientValue};\n}`;
    }
}

/**
 * 检查类名组合是否包含渐变
 * @param {string[]} group - 类名组合
 * @returns {boolean} 是否包含渐变
 */
export function hasGradientClasses(group) {
    // 检查是否有渐变方向类名
    const hasDirection = group.some(cls => {
        const decoded = utils.decodeClassNameFromUvue(cls);
        return decoded.match(/^bg-gradient-to-([trbl]|tr|tl|br|bl)$/);
    });
    
    // 检查是否有起始颜色类名
    const hasFrom = group.some(cls => {
        const decoded = utils.decodeClassNameFromUvue(cls);
        return decoded.match(/^from-.+$/);
    });
    
    // 至少需要方向和起始颜色
    return hasDirection && hasFrom;
} 